package com.patrrr.util;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.patrrr.Note.FolderBean;
import com.patrrr.Note.NoteBean;

public class NoteParse extends DefaultHandler {
	private ArrayList<NoteBean> notes;
	private ArrayList<FolderBean> folders;

	private String tag;
	private String sb;
	private String flag;

	private FolderBean folderBean;
	private NoteBean noteBean;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (flag.equals("folder")) {
			if (tag.equals("_id")) {
				folderBean.setId(Integer.parseInt(String.valueOf(ch, start, length)));
			}else if(tag.equals("name")){
				folderBean.setFolderName(String.valueOf(ch, start, length));
			}
		} else if(flag.equals("note")){
			if(tag.equals("_id")){
				noteBean.setId(Integer.parseInt(String.valueOf(ch, start, length)));
			}else if(tag.equals("folderId")){
				noteBean.setFolderId(Integer.parseInt(String.valueOf(ch, start, length)));
			}else if(tag.equals("content")){
				noteBean.setContent(String.valueOf(ch, start, length));
			}else if(tag.equals("createTime")){
				noteBean.setCreateTime(Long.parseLong(String.valueOf(ch, start, length)));
			}else if(tag.equals("alertTime")){
				noteBean.setAlertTime(Long.parseLong(String.valueOf(ch, start, length)));
			}else if(tag.equals("color")){
				noteBean.setColor(Integer.parseInt(String.valueOf(ch, start, length)));
			}else if(tag.equals("widgetId")){
				noteBean.setWidgetId(Integer.parseInt(String.valueOf(ch, start, length)));
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if(localName.equals("folder")){
			folders.add(folderBean);
		}else if(localName.equals("note")){
			notes.add(noteBean);
		}
		//Log("end--" + localName);
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		notes = new ArrayList<NoteBean>();
		folders = new ArrayList<FolderBean>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
			tag = localName;
			if (tag.equals("folder")) {
				flag=localName;
				folderBean = new FolderBean();
			} else if (tag.equals("note")) {
				flag=localName;
				noteBean = new NoteBean();
			}
			//Log("start--" + localName);
	}

	public ArrayList<NoteBean> getNotes() {
		for(int i=0;i<notes.size();i++){
			Log("_id--"+notes.get(i).getId());
			Log("\n folderId--"+notes.get(i).getFolderId());
			Log("\n content--"+notes.get(i).getContent());
			Log("\n createTime--"+notes.get(i).getCreateTime());
			Log("\n alertTime--"+notes.get(i).getAlertTime());
			Log("\n color--"+notes.get(i).getColor());
			Log("\n widgetId--"+notes.get(i).getWidgetId());
		}
		return notes;
	}

	public ArrayList<FolderBean> getFolders() {
		for(int i=0;i<folders.size();i++){
			Log(""+folders.get(i).getId());
			Log("\n"+folders.get(i).getFolderName());
		}
		return folders;
	}

	public void Log(String l) {
		android.util.Log.i("pa", l);
	}

}
